﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Models.Pager;
using App.Views.ViewModels;
namespace App.Controllers
{
    public class DeaderQueryServiceController : BaseController
    {        
        //按死者查询
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(int? page, int? rows, string order)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            //int offset = (page ?? 1 - 1) * (rows ?? 10);
            List<vi_ServiceDetail> list = null;
            //默认选择最后一个死者
            var dead = dbe.tb_RegistrationInfo.OrderByDescending(m => m.DeadNO).FirstOrDefault();
            if (dead == null)
            {
                return Json(new List<vi_ServiceDetail>(), JsonRequestBehavior.AllowGet);
            }
            if ("asc" == order)
            {
                list = dbe.vi_ServiceDetail.Where(m => m.DeadNO == dead.DeadNO).OrderBy(m => m.DeadNO).ToList();
            }
            else
            {
                list = dbe.vi_ServiceDetail.OrderByDescending(m => m.DeadNO).ToList();
            }
            decimal? sum = list.Sum(m => m.ServiceFee);
            var foot = new vi_ServiceDetail
            {
                ServiceFee = sum,
                DeadNO = "合计："
            };
            return Json(new
            {
                success = true,
                rows = list,
                footer = new object[] { foot }
            }, JsonRequestBehavior.AllowGet);

        }

        //根据死者编号查询死者费用情况
        public ActionResult Search(FormCollection collection)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            string type = Request.QueryString["type"];
            string deadNo = collection["DeadNO"];
            string deadName = collection["DeadName"];
            
            var list = dbe.vi_ServiceDetail
                .Where(m => (m.DeadName.Contains(deadName) && m.DeadNO.Contains(deadNo)));            
            if (type != "0000") {
                list = list.Where(m => m.ParentServiceID == type);
            }

            list = list.OrderBy(m => m.DeadNO);
            decimal? sum = list.Sum(m => m.ServiceFee);
            var foot = new vi_ServiceDetail
            {
                ServiceFee = sum,
                DeadNO = "合计："
            };
            return Json(new
            {
                success = true,
                rows = list,
                footer = new object[] { foot }
            }, JsonRequestBehavior.AllowGet);
        }
        
        //死者列表
        public ActionResult DeadIndex() {
            return View();
        }
        public ActionResult DeadList(int? page, int? rows, string order)
        {
            return Json(new List<vi_DeadInfos>(), JsonRequestBehavior.AllowGet);
        }
        //根据时间段和地区查询死者情况
        public ActionResult SearchByTimeAndCity(FormCollection collection)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;
            
            string deadCity = collection["DeadCity"];
            string starttime = collection["StartTime"];
            string endtime = collection["EndTime"];
            DateTime st = DateTime.MinValue;
            DateTime et = DateTime.MaxValue;
            try
            {
                st = DateTime.Parse(starttime);
            }
            catch
            {
                st = DateTime.MinValue;
            }
            try
            {
                et = DateTime.Parse(endtime);
                et = et.AddDays(1);
            }
            catch
            {
                et = DateTime.MaxValue;
            }
            //查询1 按时间或地区            
            var list = dbe.vi_DeadInfos
                .Where(m=>m.RegistDate>=st && m.RegistDate<=et);
            if (!string.IsNullOrWhiteSpace(deadCity)) {
                list = list.Where(m => m.DeadCity.Contains(deadCity));
            }

            list = list.OrderBy(m => m.DeadNO);
            int n = list.Count();
            decimal? totalfee = list.Sum(m => m.TotalFee);
            decimal? paidfee = list.Sum(m => m.PaidFee);
            var foot = new vi_DeadInfos
            {
                TotalFee = totalfee,
                PaidFee = paidfee,
                DeadNO = "合计："+ n + "人"
            };
            return Json(new
            {
                success = true,
                rows = list,
                footer = new object[] { foot }
            }, JsonRequestBehavior.AllowGet);
        }

        //打印死者费用列表
        public ActionResult PrintDeadFeeBill()
        {
            return View();
        }

        //打印死者列表
        public ActionResult PrintDeaderList()
        {
            return View();
        }
    }
}
